package edu.sysu.cshare.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.Session;
import org.hibernate.engine.CascadeStyle;

import edu.sysu.cshare.util.HibernateSessionFactory;
import edu.sysu.cshare.util.Mis;

@Entity
@Table(name = "T_HOMEWORK")
public class Homework implements Serializable {
	private Course course;
	/**
	 * 作业描述
	 */
	private String descrip;
	/**
	 * 一项作业的保存目录名 形如"数据库第一次作业"
	 */
	private String dirPath;
	/**
	 * 截止日期
	 */
	private Calendar dueDate;
	/**
	 * 上传作业时文件的类型限制 字符串形如"zip,rar,doc,docx"
	 */
	private String extRestrict;
	/**
	 * 保存到数据库时自动生成的ID
	 */
	private long id;

	/**
	 * 上传作业时文件的大小限制 KB为单位
	 */
	private int maxSize;
	private String remark;
	private Calendar startDate;
	private boolean submitByGroup;
	private List<SubmitInfo> submitInfos = new ArrayList<SubmitInfo>();

	private String submitMethod;

	public Homework() {
	}

	public Homework(Course course, Calendar startDate, Calendar dueDate,
			boolean submitByGroup, String descrip, String dirPath,
			String remark, String submitMethod, int maxSize, String extRestrict) {
		super();
		this.submitByGroup = submitByGroup;
		this.course = course;
		this.descrip = descrip;
		this.dirPath = dirPath;
		this.dueDate = dueDate;
		this.extRestrict = extRestrict;
		this.maxSize = maxSize;
		this.remark = remark;
		this.startDate = startDate;
		this.submitMethod = submitMethod;
	}

	@ManyToOne
	public Course getCourse() {
		return course;
	}

	public boolean shouldUpload(){
		return !(dirPath==null || dirPath.length()==0);
	}
	public String getDescrip() {
		return descrip;
	}

	public String getDirPath() {
		return dirPath;
	}

	public Calendar getDueDate() {
		return dueDate;
	}

	public String getExtRestrict() {
		return extRestrict;
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public int getMaxSize() {
		return maxSize;
	}

	public String getRemark() {
		return remark;
	}

	public Calendar getStartDate() {
		return startDate;
	}

	/**
	 * 与isSubmitByGroup相同
	 * 
	 * @return 是否分组提交作业
	 */
	public boolean getSubmitByGroup() {
		return submitByGroup;
	}

	public int calcSubedStuCount() {
		int cnt = 0;
		for (SubmitInfo sinfo : submitInfos) {
			if (sinfo.getSubmitDate() != null) {
				cnt++;
			}
		}
		return cnt;
	}

	public int calcSubedGroupCount() throws Exception {
		if (!submitByGroup) {
			throw new Exception("this homework is not submitted by group");
		}
		int cnt = 0;
		for (Group g : course.getGroups()) {
			if (g.getSubinfoByHomework(this).getSubmitDate() != null) {
				cnt++;
			}
		}
		return cnt;

	}

	@OneToMany(mappedBy = "homework", cascade = CascadeType.REMOVE)
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public String getSubmitMethod() {
		return submitMethod;
	}

	
	public void setCourse(Course course) {
		this.course = course;
	}

	public void setDescrip(String descrip) {
		this.descrip = descrip;
	}

	public void setDirPath(String dirPath) {
		this.dirPath = dirPath;
	}

	public void setDueDate(Calendar dueDate) {
		this.dueDate = dueDate;
	}

	public void setExtRestrict(String extRestrict) {
		this.extRestrict = extRestrict;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public void setStartDate(Calendar startDate) {
		this.startDate = startDate;
	}

	public void setSubmitByGroup(boolean submitByGroup) {
		this.submitByGroup = submitByGroup;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}

	public void setSubmitMethod(String submitMethod) {
		this.submitMethod = submitMethod;
	}

	@Override
	public String toString() {
		return "Homework [course=" + course.getName() + ", descrip=" + descrip
				+ ", dirPath=" + dirPath + ", dueDate="
				+ Mis.formatCalendar(dueDate) + ", extRestrict=" + extRestrict
				+ ", id=" + id + ", maxSize=" + maxSize + ", remark=" + remark
				+ ", startDate=" + Mis.formatCalendar(startDate)
				+ ", submitMethod=" + submitMethod + "]";
	}
}
